using System;
using System.Collections;
using System.Reflection;
using DataBroker.Dao;

namespace NorthwindDAO
{
	public class Orders : Entity
	{
		private Int32 _OrderID;
		private String _CustomerID;
		private Int32 _EmployeeID;
		private DateTime _OrderDate;
		private DateTime _RequiredDate;
		private DateTime _ShippedDate;
		private Int32 _ShipVia;
		private Decimal _Freight;
		private String _ShipName;
		private String _ShipAddress;
		private String _ShipCity;
		private String _ShipRegion;
		private String _ShipPostalCode;
		private String _ShipCountry;
	
		///Default constructor
		public Orders ()
		{
		
		}

        /// <summary>
        /// Constructor for joins. 
		/// Available CHILD tables to join with:
		///    - Order details ( Order details_fk_order_details_orders )
		/// Available PARENT tables to join with:
		///    - Customers
		///    - Employees
		///    - Shippers
        /// </summary>
        public Orders(bool Flat, params Entity[] entities)
            : this()
		{
			foreach(Entity entity in entities)
			{
				string[] entityParts = entity.GetType().ToString().Split('.');
				string entityName = entityParts[entityParts.Length - 1];
				PropertyInfo pf = GetType().GetProperty((entityName));
				if (pf != null)
				{
					pf.SetValue(this,new EntityCollection(entity), null);
				}
                entityName = entityName + "_"; ;
                pf = GetType().GetProperty((entityName));
                if (pf != null)
                {
                    pf.SetValue(this, entity, null);
                }
			}
			SetFlat(Flat);
		}
        public Orders(params Entity[] entities)
            : this(true, entities)
		{
		}
	
		[IsNotNullable]
		[IsPK]
		[IsIdentity]
		[SequenceName("Orders")]
		public Int32 Orderid
		{
			get
			{
				return _OrderID;
			}
		}
		[MaxLength(5)]
		public String Customerid
		{
			get
			{
				return _CustomerID;
			}
			set
			{
				_CustomerID = value;
			}
		}
		public Int32 Employeeid
		{
			get
			{
				return _EmployeeID;
			}
			set
			{
				_EmployeeID = value;
			}
		}
		public DateTime Orderdate
		{
			get
			{
				return _OrderDate;
			}
			set
			{
				_OrderDate = value;
			}
		}
		public DateTime Requireddate
		{
			get
			{
				return _RequiredDate;
			}
			set
			{
				_RequiredDate = value;
			}
		}
		public DateTime Shippeddate
		{
			get
			{
				return _ShippedDate;
			}
			set
			{
				_ShippedDate = value;
			}
		}
		public Int32 Shipvia
		{
			get
			{
				return _ShipVia;
			}
			set
			{
				_ShipVia = value;
			}
		}
		[DefaultValue("(0)")]
		public Decimal Freight
		{
			get
			{
				return _Freight;
			}
			set
			{
				_Freight = value;
			}
		}
		[MaxLength(40)]
		public String Shipname
		{
			get
			{
				return _ShipName;
			}
			set
			{
				_ShipName = value;
			}
		}
		[MaxLength(60)]
		public String Shipaddress
		{
			get
			{
				return _ShipAddress;
			}
			set
			{
				_ShipAddress = value;
			}
		}
		[MaxLength(15)]
		public String Shipcity
		{
			get
			{
				return _ShipCity;
			}
			set
			{
				_ShipCity = value;
			}
		}
		[MaxLength(15)]
		public String Shipregion
		{
			get
			{
				return _ShipRegion;
			}
			set
			{
				_ShipRegion = value;
			}
		}
		[MaxLength(10)]
		public String Shippostalcode
		{
			get
			{
				return _ShipPostalCode;
			}
			set
			{
				_ShipPostalCode = value;
			}
		}
		[MaxLength(15)]
		public String Shipcountry
		{
			get
			{
				return _ShipCountry;
			}
			set
			{
				_ShipCountry = value;
			}
		}
	
        #region Order details 
        private EntityCollection _Order_details;
        [NotInTable]
        [TypeContained(typeof(Order_details),"Order details")]
        public EntityCollection Order_details
        {
            get
            {
                return _Order_details;
            }

            set
            {
                _Order_details = value;
            }
        }
        private Order_details _Order_details_;
        [NotInTable]
        public Order_details Order_details_
        {
            get
            {
                return _Order_details_;
            }

            set
            {
                _Order_details_ = value;
            }
        }
        #endregion
        #region Customers 
        private EntityCollection _Customers;
        [NotInTable]
        [TypeContained(typeof(Customers),"Customers")]
        public EntityCollection Customers
        {
            get
            {
                return _Customers;
            }

            set
            {
                _Customers = value;
            }
        }
        private Customers _Customers_;
        [NotInTable]
        public Customers Customers_
        {
            get
            {
                return _Customers_;
            }

            set
            {
                _Customers_ = value;
            }
        }
        #endregion
        #region Employees 
        private EntityCollection _Employees;
        [NotInTable]
        [TypeContained(typeof(Employees),"Employees")]
        public EntityCollection Employees
        {
            get
            {
                return _Employees;
            }

            set
            {
                _Employees = value;
            }
        }
        private Employees _Employees_;
        [NotInTable]
        public Employees Employees_
        {
            get
            {
                return _Employees_;
            }

            set
            {
                _Employees_ = value;
            }
        }
        #endregion
        #region Shippers 
        private EntityCollection _Shippers;
        [NotInTable]
        [TypeContained(typeof(Shippers),"Shippers")]
        public EntityCollection Shippers
        {
            get
            {
                return _Shippers;
            }

            set
            {
                _Shippers = value;
            }
        }
        private Shippers _Shippers_;
        [NotInTable]
        public Shippers Shippers_
        {
            get
            {
                return _Shippers_;
            }

            set
            {
                _Shippers_ = value;
            }
        }
        #endregion
        public override DBJoin GetJoin(string tableName)
        {
			if (string.Compare(tableName, "Order_Details", true) == 0)
			{
				ArrayList left = new ArrayList();
				ArrayList right = new ArrayList();
				left.Add(tableName + ".Orderid");
				right.Add(GetType().Name + ".Orderid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
	        if (string.Compare(tableName, "Customers", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Customerid");
				right.Add(GetType().Name + ".Customerid");
				return new DBJoin(left, right, JoinTypeEnumeration.INNER);
			}
	        if (string.Compare(tableName, "Employees", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Employeeid");
				right.Add(GetType().Name + ".Employeeid");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
	        if (string.Compare(tableName, "Shippers", true) == 0)
            {
                ArrayList left = new ArrayList();
                ArrayList right = new ArrayList();
				left.Add(tableName + ".Shipperid");
				right.Add(GetType().Name + ".Shipvia");
				return new DBJoin(left, right, JoinTypeEnumeration.LEFT_OUTER);
			}
			return null;
		}
	}
}


